//爬取鲜花网的列表页数据，转存到本地的数据库-mongoDB
const request = require("request"); //发送ajax请求
const cheerio = require("cheerio"); //jq的节点操作
const fs = require("fs"); //文件模块-系统内置的
var iconv = require("iconv-lite"); //转码的模块
let mongo = require("./db/mongo");
let path = require("path"); //处理路径的模块，核心模块
let config = require("./config.json");

for (let i = 1; i <= 5; i++) {
  request(
    { url: "https://www.hua.com/flower/?r=0&pg=" + i, encoding: "utf-8" },
    (req, res, body) => {
      $ = cheerio.load(body);
      // console.log(body);
      let arr = []; //[{},{}]
      $(".grid-wrapper .grid-item").each((index, item) => {
        let obj = {};
        //分析节点，获取你想要的数据
        let url = $(item).find(".img-box img").attr("src");
        url = "http:" + url;
        let filename = path.basename(url); //获取路径后面的文件名
        // console.log(__dirname); //当前服务器所在目录
        request(url).pipe(fs.createWriteStream("./img/" + filename)); //发请求下载图片资源
        let title = $(item).find(".product-title").text();

        //http://localhost:2005/img/9010668.jpg_220x240.jpg
        let newurl = config.downloadUrl + filename;
        obj = {
          url: newurl,
          title,
        };
        arr.push(obj);
      });

      //   console.log(arr, 999); //[{},{},{}]
      mongo.insert({
        colname: "goods",
        data: arr,
      });
    }
  );
}
